﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;

namespace OA.Controllers
{
    public class OperatorController : Controller
    {
        //
        // GET: /Operator/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Operator()
        {
            return View();
        }
        
        //加载数据
        public ActionResult Load()
        {
            operatorInfo OI = new operatorInfo();
            operatorInfoManager OIManager = new operatorInfoManager();
            StringBuilder sb = new StringBuilder();
            List<operatorInfo> list  = OIManager.GetAll().ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                sb.Append("<tr ondblclick=\"editRole(" + list[i].id + ")\" onclick=\"selectRole(" + list[i].id + ")\" onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                sb.Append("<td>"+list[i].id+"</td>");
                sb.Append("<td>" + list[i].username + "</td>");
                sb.Append("<td>" + list[i].name + "</td>");
                if (Convert.ToBoolean(list[i].sex))
                {
                    sb.Append("<td>男</td>");
                }
                else
                {
                    sb.Append("<td>女</td>");
                }
                sb.Append("<td>" + list[i].phone + "</td>");
                if (list[i].roleId == 13)
                {
                    sb.Append("<td>系统管理员</td>");
                }
                else
                {
                    sb.Append("<td>普通管理员</td>");
                }
                sb.Append("<td><a href=\"#\" onclick=\"editRole(" + list[i].id + ")\">编 辑</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"delOperator(" + list[i].id + ")\">删 除</a></td>");
                sb.Append("</tr>");
            }
             return Content(sb.ToString());
        }
        
        //编辑角色
        public ActionResult editRole()
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return Content("<script>window.parent.location.href='/Login/Login';</script>");
            }
            //string RoleId = (System.Web.HttpContext.Current.User.Identity as FormsIdentity).Ticket.UserData;
            //ViewData["RoleId"] = RoleId;
            string Id = System.Web.HttpContext.Current.Request.QueryString["id"];
            if (!string.IsNullOrEmpty(Id))
            {
                operatorInfo OI = new operatorInfo();
                operatorInfoManager OIManager = new operatorInfoManager();
                OI = OIManager.GetByID(Convert.ToInt32(Id));
                ViewData["OperatorId"] = Id;
                ViewData["userName"] = OI.username;
                ViewData["name"] = OI.name;
                List<SelectListItem> role = new List<SelectListItem>();
                if (OI.roleId == 13)
                {
                    role.Add(new SelectListItem { Text = "系统管理员", Value = "13", Selected = true });
                    role.Add(new SelectListItem { Text = "普通管理员", Value = "14" });
                }
                else
                {
                    role.Add(new SelectListItem { Text = "系统管理员", Value = "13" });
                    role.Add(new SelectListItem { Text = "普通管理员", Value = "14", Selected = true });
                }
                ViewData["role"] = role;
                List<SelectListItem> Sex = new List<SelectListItem>();
                if (Convert.ToBoolean(OI.sex))
                {
                    Sex.Add(new SelectListItem { Text = "男", Value = "true", Selected = true });
                    Sex.Add(new SelectListItem { Text = "女", Value = "false" });
                }
                else
                {
                    Sex.Add(new SelectListItem { Text = "男", Value = "true" });
                    Sex.Add(new SelectListItem { Text = "女", Value = "false", Selected = true });
                }
                ViewData["sex"] = Sex;
                ViewData["phone"] = OI.phone;
                ViewData["radio"] = false;
                return View();
            }
            else
            {
                List<SelectListItem> role = new List<SelectListItem>();
                
                role.Add(new SelectListItem { Text = "普通管理员", Value = "14" });
                role.Add(new SelectListItem { Text = "系统管理员", Value = "13"});

                List<SelectListItem> Sex = new List<SelectListItem>();
                Sex.Add(new SelectListItem { Text = "男", Value = "true"});
                Sex.Add(new SelectListItem { Text = "女", Value = "false" });
                ViewData["OperatorId"] = "0";
                ViewData["role"] = role;
                ViewData["sex"] = Sex;
                return View();

            } 
        }

        [HttpPost]
        public ActionResult editRole(FormCollection fc)
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return Content("<script>window.parent.location.href='/Login/Login';</script>");
            }
            string OperatorId = fc["OperatorId"];
            if (!string.IsNullOrEmpty(OperatorId) && OperatorId != "0")
            {
                string userName = fc["userName"];
                string name = fc["name"];
                string role = fc["role"];
                string sex = fc["sex"];
                string phone = fc["phone"];
                string radio = fc["radio"];
                string admin_NewPwd = fc["admin_NewPwd"];
                string admin_Con_Pwd = fc["admin_Con_Pwd"];
                operatorInfo OI = new operatorInfo();
                operatorInfoManager OIManager = new operatorInfoManager();
                OI = OIManager.GetByID(Convert.ToInt32(OperatorId));
                if (OI != null)
                {
                    OI.username = userName;
                    OI.name = name;
                    OI.roleId = Convert.ToInt32(role);
                    OI.sex = Convert.ToBoolean(sex);
                    OI.phone = phone;
                    if (admin_NewPwd != "0" && admin_Con_Pwd != "0" && admin_NewPwd == admin_Con_Pwd)
                    {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        byte[] bt = Encoding.Default.GetBytes(admin_NewPwd);
                        bt = md5.ComputeHash(bt);
                        string pwd = BitConverter.ToString(bt).Replace("-", "");
                        OI.password = pwd;
                    }
                    OIManager.Update();
                    return Content("<script>alert('更新成功');window.parent.location.href='/Operator/Operator';parent.layer.close(parent.layer.getFrameIndex());</script>");
                }
                return Content("<script>alert('未找到要修改的对象,请重新选择要修改的用户');window.parent.location.href='/Operator/Operator';parent.layer.close(parent.layer.getFrameIndex());</script>");
            }
            else
            {
                string userName = fc["userName"];
                string name = fc["name"];
                string role = fc["role"];
                string sex = fc["sex"];
                string phone = fc["phone"];
                string radio = fc["radio"];
                string admin_NewPwd = fc["admin_NewPwd"];
                string admin_Con_Pwd = fc["admin_Con_Pwd"];
                operatorInfo OI = new operatorInfo();
                operatorInfoManager OIManager = new operatorInfoManager();
                OI.username = userName;
                OI.name = name;
                OI.roleId = Convert.ToInt32(role);
                OI.sex = Convert.ToBoolean(sex);
                OI.phone = phone;
                if (admin_NewPwd != "0" && admin_Con_Pwd != "0" && admin_NewPwd == admin_Con_Pwd)
                {
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] bt = Encoding.Default.GetBytes(admin_NewPwd);
                    bt = md5.ComputeHash(bt);
                    string pwd = BitConverter.ToString(bt).Replace("-", "");
                    OI.password = pwd;
                    OIManager.Add(OI);
                    OIManager.Update();
                    return Content("<script>alert('添加成功');window.parent.location.href='/Operator/Operator';parent.layer.close(parent.layer.getFrameIndex());</script>");
                }
                else
                {
                    return Content("<script>alert('密码设置有误,请重新添加');window.parent.location.href='/Operator/Operator';parent.layer.close(parent.layer.getFrameIndex());</script>");
                }
            }
        } 

        //删除角色
        public ActionResult delRole(FormCollection fc)
        {
            operatorInfo OI = new operatorInfo();
            operatorInfoManager OIManager = new operatorInfoManager();

            string Id = fc["Id"];
            if (!string.IsNullOrEmpty(Id))
            {
                OI = OIManager.GetByID(Convert.ToInt32(Id));
                if (OI != null)
                {
                    OIManager.Delete(OI);
                    OIManager.Update();
                    return Content("删除成功");
                }
            }
            return Content("删除失败,未能找到该用户");
        }

    }
}
